package me.support.exception;

import lombok.extern.slf4j.Slf4j;
import me.support.enums.ResultCode;
import me.support.model.CommonResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.text.ParseException;
import java.util.stream.Collectors;

/**
 * @author felix
 * @date 2022/1/9/0009
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public CommonResult<String> exception(Exception e) {
        log.error("全局异常信息 msg={}",e.getMessage(),e);
        return CommonResult.fail(ResultCode.FAILED.getCode(),e.getMessage());
    }

    @ExceptionHandler(ParseException.class)
    public CommonResult<String> parseException(ParseException e) {
        log.error("编译异常 msg={}",e.getMessage(),e);
        return CommonResult.fail(ResultCode.VALIDATE_FAILED.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    public ResponseEntity<CommonResult<String>> handleValidatedException(Exception e) {
        CommonResult<String> resp = null;

        if (e instanceof MethodArgumentNotValidException) {
            // BeanValidation exception
            log.error("全局异常信息 msg={}",e.getMessage(),e);
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            resp = CommonResult.fail(HttpStatus.BAD_REQUEST.value(),
                    ex.getBindingResult().getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.joining("; "))
            );
        } else if (e instanceof ConstraintViolationException) {
            // BeanValidation GET simple param
            log.error("全局异常信息 msg={}",e.getMessage(),e);
            ConstraintViolationException ex = (ConstraintViolationException) e;
            resp = CommonResult.fail(HttpStatus.BAD_REQUEST.value(),
                    ex.getConstraintViolations().stream()
                            .map(ConstraintViolation::getMessage)
                            .collect(Collectors.joining("; "))
            );
        } else if (e instanceof BindException) {
            // BeanValidation GET object param
            log.error("全局异常信息 msg={}",e.getMessage(),e);
            BindException ex = (BindException) e;
            resp = CommonResult.fail(HttpStatus.BAD_REQUEST.value(),
                    ex.getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.joining("; "))
            );
        }

        return new ResponseEntity<>(resp,HttpStatus.BAD_REQUEST);
    }

}
